#include<stdio.h>
#include "struct.h"


void main()
{
    FILE *fin,*fout;
    Header headfirst;
    Infoheader headsecond;
    int EndByte;
    typedef unsigned short WORD;
    unsigned char blue,green,red;
    int i,j,grayval,c;
    unsigned short display[200][200];
    unsigned short image_rot_90[200][200];


    fin = fopen("cb.bmp","rb");
    if(fin==NULL)
    {
        printf("Error opening first file");
        exit(0);
    }

    fout = fopen("cb1.bmp","wb");
    if(fout==NULL)
    {
        printf("Error opening first file");
        exit(0);
    }


    fread(&headfirst,sizeof(headfirst),1,fin);

//printf("%x\n",headfirst.type);
//printf("%d\n",headfirst.size);
//printf("%u\n",headfirst.offset);
    fread(&headsecond,sizeof(headsecond),1,fin);
//printf("%d\n",headsecond.size);
//printf("%d\n",headsecond.width);
//printf("%d\n",headsecond.height);
//printf("%x\n",headsecond.plane);
//printf("%d\n",headsecond.bitcount);
//printf("%u\n",headsecond.compression);


//printf("sizeof headfirst %d \n sizeof headsecond %d\n",sizeof(headfirst),sizeof(headsecond));
//printf("**%d\n",sizeof(unsigned short int));

    fseek(fin,(headfirst.offset),SEEK_SET);
    if(headsecond.imagesize==0)
    {
        headsecond.imagesize=headfirst.size;
    }

    int bm_width=(WORD)(headsecond.width);

    int bm_height=(WORD)(headsecond.height);

    if (headsecond.imagesize-bm_width*bm_height*3==0)
        EndByte=0;
    else
        EndByte=(headsecond.imagesize-bm_width*bm_height*3)/bm_height;

//printf("%d",EndByte);


    for(i=0; i<bm_height; i++)
    {
        for(j=0; j<bm_width; j++)
        {
            blue=getc(fin);

            green=getc(fin);

            red=getc(fin);

            grayval=(green+red+blue)/3;

            display[i][j]=grayval;
        }
    }



    for(i=0; i<bm_height-EndByte; i++)
    {
        for(j=0; j<bm_width; j++)
        {
            printf("%x",display[i][j]);
        }
        printf("\n");
    }

    c=headsecond.width;
    headsecond.width=headsecond.height;
    headsecond.height=c;

    fwrite(&headfirst,sizeof(headfirst),1,fout);
    fwrite(&headsecond,sizeof(headsecond),1,fout);

    /*
    headfirst1.offset=headerfirst.offset;
    headfirst1.reserved1=headfirst.reserved1;
    headfirst1.reserved2=headfirst.reserved2;
    headfirst1.size=headfirst.size;
    headfirst1.type=headfirst.type;

    headsecond1.bitcount=headsecond.bitcount;
    headsecond1.compression=headsecond.compression;
    headsecond1.height=headsecond.width;
    headsecond1.imagesize=headsecond.imagesize;
    headsecond1.importantcolors=headsecond.importantcolors;
    headsecond1.ncolors=headsecond.ncolors;
    headsecond1.plane=headsecond.plane;
    headsecond1.size=headsecond.size;
    headsecond1.width=headsecond.height;
    headsecond1.xresolution=headsecond.xresolution;
    headsecond1.yresolution=headsecond.yresolution;
    */


    for(i=0; i<headsecond.width; i++)
    {
        for(j=0; j<headsecond.height; j++)
        {
            image_rot_90[i][j]=display[headsecond.height-1-j][i];
        }
    }

    fseek(fout,(headfirst.offset),SEEK_SET);

    for(i=0; i<headsecond.height; i++)
    {
        for(j=0; j<headsecond.width; j++)
        {
            fwrite(&image_rot_90[i][j],sizeof(unsigned short),1,fout);
        }
    }


    fclose(fout);
    fclose(fin);
}





